package com.x.erp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.x.erp.model.CarAge;
import com.x.erp.model.CarMileage;
import com.x.erp.model.CarSelectTemplate;
import com.x.erp.model.City;
import com.x.erp.model.Series;
import com.x.erp.rpc.CarSelectTemplateRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 车辆查询模板订阅信息
 */
@RestController
@RequestMapping(value = "/carSelectTemplate")
public class CarSelectTemplateController extends BaseController {

	@Reference
	private CarSelectTemplateRpcService carSelectTemplateRpcService;

	/**
	 * 新增车辆查询模板订阅信息
	 * 
	 * @param carSelectTemplate
	 * @return
	 */
	@RequestMapping(value = "/insertCarSelectTemplate", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertCarSelectTemplate(CarSelectTemplate carSelectTemplate, String templateJson) {
		if (StringUtils.isEmpty(templateJson)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "订阅信息不能为空！");
		}
		if (carSelectTemplate == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "订阅信息不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "市场ID不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "商铺ID不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "创建人ID不能为空！");
		}
		carSelectTemplate.setTemplateCreateTime(new Date());
		List<CarSelectTemplate> list = new ArrayList<>();
		JSONObject obj = JSON.parseObject(templateJson);
		JSONArray seriesArray = obj.getJSONArray("series");
		JSONArray cityArray = obj.getJSONArray("city");
		JSONArray carAgeArray = obj.getJSONArray("carAge");
		JSONArray carMileageArray = obj.getJSONArray("carMileage");
		if(!addCarSelectTemplateToList(list, seriesArray, carSelectTemplate, "series_id", "seriesId", "seriesName")){
			return new ResultModel(ResultCode.RESULT_FAIL, "数据格式不正确！");
		}
		if(!addCarSelectTemplateToList(list, cityArray, carSelectTemplate, "city_id", "cityId", "cityName")){
			return new ResultModel(ResultCode.RESULT_FAIL, "数据格式不正确！");
		}
		if(!addCarSelectTemplateToList(list, carAgeArray, carSelectTemplate, "car_age_id", "carAgeId", "carAgeRemark")){
			return new ResultModel(ResultCode.RESULT_FAIL, "数据格式不正确！");
		}
		if(!addCarSelectTemplateToList(list, carMileageArray, carSelectTemplate, "car_mileage_id", "carMileageId", "carMileageRemark")){
			return new ResultModel(ResultCode.RESULT_FAIL, "数据格式不正确！");
		}
		carSelectTemplateRpcService.insertCarSelectTemplate(carSelectTemplate, list);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}
	
	/**
	 * 查询车辆查询模板订阅列表
	 * 
	 * @param carSelectTemplate
	 * @return
	 */
	@RequestMapping(value = "/getCarSelectTemplateList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarSelectTemlateList(CarSelectTemplate carSelectTemplate){
		if (carSelectTemplate == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "订阅信息不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "市场ID不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "商铺ID不能为空！");
		}
		List<CarSelectTemplate> selectCarSelectTemplateList = carSelectTemplateRpcService.selectCarSelectTemplateList(carSelectTemplate);
		Map<String, List<Object>> map = fromListToMap(selectCarSelectTemplateList);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！",map);
	}

	/**
	 * 删除车辆查询模板订阅信息
	 * 
	 * @param carSelectTemplate
	 * @return
	 */
	@RequestMapping(value = "/deleteCarSelectTemplate", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deleteCarSelectTemplate(CarSelectTemplate carSelectTemplate) {
		if (carSelectTemplate == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "订阅信息不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "市场ID不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "商铺ID不能为空！");
		}
		if (StringUtils.isEmpty(carSelectTemplate.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "删除操作人ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！",
				carSelectTemplateRpcService.deleteCarSelectTemplate(carSelectTemplate));
	}

	private boolean addCarSelectTemplateToList(List<CarSelectTemplate> ts, JSONArray jsonArray, CarSelectTemplate carSelectTemplate, String key ,String value,String remark){
		if(jsonArray != null){
			for(int i = 0; i < jsonArray.size(); i++){
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				CarSelectTemplate tem = new CarSelectTemplate();
				tem.setTemplateCreateTime(carSelectTemplate.getTemplateCreateTime());
				tem.setMarketId(carSelectTemplate.getMarketId());
				tem.setShopId(carSelectTemplate.getShopId());
				tem.setUserId(carSelectTemplate.getUserId());
				tem.setTemplateKey(key);
				String keyStr = String.valueOf(jsonObject.get(value));
				String valStr = String.valueOf(jsonObject.get(remark));
				if(StringUtils.isBlank(keyStr) || StringUtils.isBlank(valStr)){
					return false;
				}
				tem.setTemplateValue(keyStr);
				tem.setTemplateRemark(valStr);
				ts.add(tem);
			}
		}
		return true;
	}
	
	private Map<String,List<Object>> fromListToMap(List<CarSelectTemplate> selectCarSelectTemplateList){
		List<Object> series = new ArrayList<>();
		List<Object> city = new ArrayList<>();
		List<Object> carAge = new ArrayList<>();
		List<Object> carMileage = new ArrayList<>();
		Series seriesNew = null;
		City cityNew = null;
		CarAge carAgeNew = null;
		CarMileage carMileageNew = null;
		for (CarSelectTemplate tem : selectCarSelectTemplateList) {
			switch (tem.getTemplateKey()) {
			case "series_id":
				seriesNew = new Series();
				seriesNew.setSeriesId(Integer.valueOf(tem.getTemplateValue()));
				seriesNew.setSeriesName(tem.getTemplateRemark());
				series.add(seriesNew);
				break;
			case "city_id":
				cityNew = new City();
				cityNew.setCityId(tem.getTemplateValue());
				cityNew.setCityName(tem.getTemplateRemark());
				city.add(cityNew);
				break;
			case "car_age_id":
				carAgeNew = new CarAge();
				carAgeNew.setCarAgeId(Integer.valueOf(tem.getTemplateValue()));
				carAgeNew.setCarAgeRemark(tem.getTemplateRemark());
				carAge.add(carAgeNew);
				break;
			case "car_mileage_id":
				carMileageNew = new CarMileage();
				carMileageNew.setCarMileageId(Integer.valueOf(tem.getTemplateValue()));
				carMileageNew.setCarMileageRemark(tem.getTemplateRemark());
				carMileage.add(carMileageNew);
				break;

			default:
				break;
			}
		}
		Map<String,List<Object>> map = new HashMap<>();
		map.put("series", series);
		map.put("city", city);
		map.put("carAge", carAge);
		map.put("carMileage", carMileage);
		return map;
	}
}
